//
//  WBTabBar.swift
//  sinaWeibo
//
//  Created by 吴熠 on 16/11/12.
//  Copyright © 2016年 吴熠. All rights reserved.
//

import UIKit

class WBTabBar: UITabBar {
    
    //定义闭包来传递点击事件到控制器
    var composeClosure:(() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加撰写的item
        self.addSubview(composeBtn)
        //监听按钮点击事件
        composeBtn.addTarget(self, action: #selector(composeBtnDidClick), for: .touchUpInside)
    }
    
    @objc private func composeBtnDidClick(){
        composeClosure?()
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    //重写layoutsubviews布局tabbaritem
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = UIScreen.main.bounds.width/5
        let h = self.bounds.height
        var index = 0
        for subview in subviews{
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                //修改frame
                subview.frame = CGRect(x: CGFloat(index)*w, y: 0, width: w, height: h)
                
                index += (index == 1 ? 2 : 1)
                
            }
        }
        //设置撰写按钮大小
        composeBtn.bounds.size = CGSize(width: w, height: h)
        composeBtn.center = CGPoint(x: self.center.x, y: h*0.5)
        
        
    }
    //懒加载撰写按钮
    lazy var composeBtn : UIButton = {
        let btn = UIButton()
        //设置button图片
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        //设置背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return btn
    }()

    

}
